package com.sunnyatwal.kettlebell.sounds;

import java.util.Arrays;

import com.sunnyatwal.kettlebell.model.Exercise;

public class BeepManager {
	
	private BeepIDManager mBeeps;
	private boolean mIsFinished = false;
	
	private int mTimesToBeepCountdownSorted[] = { 1000, 2000, 3000 };
	
	
	public BeepManager(BeepIDManager beeps) {
		mBeeps = beeps;
	}
	
	
	public void checkForBeeps(Exercise exercise) {
		
		if (mIsFinished) {
			return;
		}
		
		int remainingTime = exercise.getRemainingTimeCurrentExercise();
		
		BeepID id = BeepID.INVALID;
		
		if (exercise.isFinished()) {
			id = BeepID.COMPLETE;
			mIsFinished = true;
		}		
		else if (remainingTime == 100) {
			id = BeepID.NEXT;
		}
		else if (Arrays.binarySearch(mTimesToBeepCountdownSorted, remainingTime) > -1) {
			id = BeepID.COUNTDOWN;			
		}
		
		if (id != BeepID.INVALID ) {	
			
			try {
				mBeeps.playBeep(id);
			} catch (Exception e) {
				e.printStackTrace();
			}		
		}
		
	}
	
	
}
